package editdomain2;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use base 'createdomain2', 'editdomain', 'func';
use Data::Validate::Domain qw(is_domain);
use Data::Validate::Email qw(is_email);
use strict;
use CGI::Carp qw(fatalsToBrowser);

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub valid_param {
    my $self = shift;
    $self->SUPER::valid_param(@_);
    return $self;
}

sub valid_param2_edit {
    my $self = shift;

    if (   ( $self->{dd}->{m_domain} != 0 )
        && ( $self->{dd}->{m_domain} < $self->{dd}->{c_domain} )
        && ( $self->{session}->{name} ne 'Admin' ) )
    {
        $self->{he}->{fDomain_limit} = 1;
    }

    if ( $self->{he}->{fDomain_correct} != 1 ) {
        my $cl;
        my @sql = ();
        if ( $self->{session}->{name} ne 'Admin' ) {
            $cl = "AND idnane = ?";
            push @sql, $self->{p}->{fDomain}, $self->{session}->{user};
        }
        else {
            push @sql, $self->{p}->{fDomain};
        }

        my $sth = $self->{dbh}->prepare(
            "SELECT domain
                         FROM domain 
                         WHERE domain = ?
                         $cl
                         LIMIT 1  
                             "
        );
        $sth->execute(@sql);
        my $domain_sql = $sth->fetchrow_array();
        $sth->finish();

        if ( !defined($domain_sql) ) {
            $self->{he}->{fDomain_no_exist} = 1;
        }

    }

    return $self;
}

sub edit_domain {
    my $self = shift;

    my $fount;
    foreach my $value ( values( %{ $self->{he} } ) ) {
        if ( $value == 1 ) { $fount = 1; last; }
    }

    if ( !$fount ) {

        my $sth = $self->{dbh}->prepare( '
                            UPDATE domain 
                            SET description = ?,
                            aliases = ?,
                            mailboxes = ?,
                            maxquota = ?,
                            transport = ?,
                            backupmx = ?,
                            modified = DATETIME("NOW") 

                            WHERE domain = ?
              ' );

        $sth->execute(
            $self->{p}->{fDescription},    $self->{p}->{fAliases},
            $self->{p}->{fMailboxes},      $self->{p}->{fMaxquota},
            $self->{se}->{transport_user}, $self->{p}->{fBackupmx},
            $self->{p}->{fDomain},
        );
        $sth->finish();

        $self->{he}->{domain_added} = 1;
        $self->{he}->{domain}       = $self->{p}->{fDomain};
        return $self;
    }

}

sub return_edit_domain {
    my $self = shift;
    $self->{p}->{domain} = $self->{p}->{fDomain};
    $self->SUPER::return_edit_domain(@_);
    return $self;
}

sub templ_hash_domain {
    my $self = shift;

    $self->{session}->{filet} = 'edit-domain';
    $self->SUPER::templ_hash(@_);

}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
